package MultiThread;

public class q1114_Foo {
    private volatile int sign = 1;
    private final Object obj = new Object();
    public q1114_Foo() {

    }

    public void first(Runnable printFirst) throws InterruptedException {
        synchronized (obj) {
            while (sign != 1) obj.wait();
            printFirst.run();
            sign = 2;
            obj.notifyAll();
        }

    }

    public void second(Runnable printSecond) throws InterruptedException {
        synchronized (obj) {
            while (sign != 2) obj.wait();
            printSecond.run();
            sign = 3;
            obj.notifyAll();
        }

        // printSecond.run();
    }

    public void third(Runnable printThird) throws InterruptedException {
        synchronized (obj) {
            while (sign != 3) obj.wait();
            printThird.run();
            // sign = 3;
            obj.notifyAll();
        }

        // printThird.run();
    }
}
